// pages/productDetails/productDetails.js
const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    numbers:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.shop_id){
      this.setData({
        shop_id: options.shop_id
      })
    }

    if(options.uid){
      this.setData({
        uid: options.uid
      })
    }

    if (options.sku_code) {
      this.setData({
        sku_code: options.sku_code
      })
    }
    this.getProductInfo();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  getProductInfo: function () {
    var that = this;
    var params = new Object()
    params.sku_code = this.data.sku_code;
    if (this.data.shop_id){
      params.shop_id = this.data.shop_id;
    }
    util.showLoading("数据加载中..");
    api.POST({
      params: params,
      url: app.globalData.base_url + 'goods/getInfo',
      success: function (res) {
        util.hiddenLoading();
        if (util.isEmpty(res.data.code)) {
          that.setData({
            rawData: res.data.data,
          });
        }

      },
      fail: function (fail) {
        util.showErrorPath("../../../", "获取失败，请检查网络");
      }
    })
  },
  addNum:function () {
    var num = this.data.numbers;
    num++;
    this.setData({
      numbers: num
    })
  },
  minusNum:function () {
    var num = this.data.numbers;
    if (num > 1) {
      num--;
    }
    this.setData({
      numbers: num
    })
  },
  // 加入购物车
  addCart:function(){
    var that = this;
    var params = new Object()
    params.sku_code = this.data.sku_code;
    params.num = this.data.numbers;
    if (this.data.shop_id) {
      params.shop_id = this.data.shop_id;
    }
    api.POST({
      params: params,
      url: app.globalData.base_url + 'shoppingcart/addGoods',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          util.showStatus("加入成功")
        }else{
          util.showStatus(res.data.message)
        }

      },
      fail: function (fail) {
        util.showErrorPath("../../../", "获取失败，请检查网络");
      }
    })
  },
  // 立即购买
  pay:function(){
    var orderList =[];
    var box  = new Object();
    box.num = this.data.numbers;
    box.stock = this.data.rawData.stock;
    if (this.data.shop_id) {
      box.shop_id = this.data.shop_id;
      var shop_id = this.data.shop_id;
      if (box.num > box.stock){
        util.showStatus("库存不足");
        return;
      }
    }
    if(this.data.uid){
      var uid = this.data.uid;
    }else{
      var uid =''
    }
    
    box.sku_code = this.data.sku_code;
    box.current_price = this.data.rawData.current_price;
    box.gname = this.data.rawData.gname;
    box.image = this.data.rawData.image;
    box.degree = this.data.rawData.degree;
    box.degree_unit = this.data.rawData.degree_unit;
    box.volume = this.data.rawData.volume;
    box.volume_unit = this.data.rawData.volume_unit;
    box.total = (this.data.numbers * this.data.rawData.current_price).toFixed(2);
    orderList.push(box);
    wx.setStorageSync('orderList', orderList);
    if(shop_id){
      wx.navigateTo({
        url: '../order/order?shop_id=' + shop_id
      })
    } else if (uid){
      wx.navigateTo({
        url: '../order/order?uid=' + uid
      })
    }else{
      wx.navigateTo({
        url: '../order/order'
      })
    }
  },
  linkCart: function () {
    wx.switchTab({
      url: '../cart/cart'
    })
  },
  onShareAppMessage: function (res) {
    var that = this;
      var uid = '&uid=' + app.globalData.accountInfo.uid;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title:that.data.rawData.gname,
        path: '/pages/productDetails/productDetails?sku_code=' + that.data.sku_code + uid,
        imageUrl: that.data.rawData.cover,
        success: function (res) {
          // 转发成功
        },
        fail: function (res) {
          // 转发失败
        }
      }
    }
  }

})